/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.worker.app;

import org.openislands.oi.config.MyNodeInfo;
import org.openislands.oi.constant.RoleType;
import org.openislands.oi.constant.MessageCodeEnum;
import org.openislands.oi.error.LogicException;
import org.openislands.oi.operator.RuntimeConfig;
import org.openislands.oi.pojo.dto.OfNodeTaskDTO;
import org.openislands.oi.pojo.dto.CommonResult;
import org.openislands.oi.worker.scheduler.OperatorRuntimeManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class OperatorRuntimeService {
    @Resource
    private OperatorRuntimeManager operatorRuntimeManager;
    @Resource
    private MyNodeInfo nodeInfo;

    public CommonResult<RuntimeConfig> getRuntimeConfig(String jobId, String taskName, Integer taskVersion, RoleType role) {
        try {
            RuntimeConfig result = operatorRuntimeManager.getRuntimeConfig(jobId, taskName, taskVersion, role, nodeInfo.getNodeId());
            return CommonResult.success(result);
        } catch (LogicException e) {
            return CommonResult.error(e.getResponseMessageCode(), e.getMessage());
        }
    }

    public CommonResult<Boolean> updateRuntimeState(String jobId, String taskName, Integer taskVersion, RoleType role, OfNodeTaskDTO ofNodeTaskDTO) {
        try {
            boolean st = operatorRuntimeManager.updateRuntimeState(jobId, taskName, taskVersion, role, nodeInfo.getNodeId(), ofNodeTaskDTO);
            if (st) {
                return CommonResult.success();
            } else {
                return CommonResult.error(MessageCodeEnum.DATA_UPDATE_ERROR);
            }
        } catch (LogicException e) {
            return CommonResult.error(e.getResponseMessageCode(), e.getMessage());
        }
    }

    public CommonResult<Boolean> checkNextState(String jobId, String taskName, Integer taskVersion, RoleType role) {
        try {
            boolean st = operatorRuntimeManager.checkNextState(jobId, taskName, taskVersion, role, nodeInfo.getNodeId());
            if (st) {
                return CommonResult.success();
            } else {
                return CommonResult.error(MessageCodeEnum.EXECUTION_ERROR);
            }
        } catch (LogicException e) {
            return CommonResult.error(e.getResponseMessageCode(), e.getMessage());
        }
    }

    public CommonResult<Boolean> updateHeartbeat(String jobId, String taskName, Integer taskVersion, RoleType role, OfNodeTaskDTO ofNodeTaskDTO) {
        try {
            ofNodeTaskDTO.setNodeId(nodeInfo.getNodeId());
            boolean st = operatorRuntimeManager.updateHeartbeat(jobId, taskName, taskVersion, role, nodeInfo.getNodeId(), ofNodeTaskDTO);
            if (st) {
                return CommonResult.success();
            } else {
                return CommonResult.error(MessageCodeEnum.DATA_UPDATE_ERROR);
            }
        } catch (LogicException e) {
            return CommonResult.error(e.getResponseMessageCode(), e.getMessage());
        }
    }
}
